package com.lczc.soa.controller;

import com.lczc.soa.domain.Admin;
import com.lczc.soa.domain.SearchAdminResponse;
import com.lczc.soa.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/Admin")
public class AdminController {

	//wolaiye

    @Autowired
    private AdminService adminService;

    @RequestMapping("/tologin")
    public String tologin(){
        return "admin_login";
    }

    @RequestMapping("/login")
    public String login(Model model, String name, String password, HttpSession session){
        Admin admin = adminService.findAdminByName(name);
        String msg = null;
//        System.out.println(admin.getName() + "," + admin.getPassword());
        if(admin == null){
            msg = "用户名不存在";
            model.addAttribute("msg",msg);
            return "admin_login";
        }else {
            String pwd = admin.getPassword();
            if (pwd!=null && pwd.equals(password)){
                msg = "登录成功";
                model.addAttribute("msg",msg);
                session.setAttribute("current_user",admin);
                return "login_success";
            }else{
                msg = "密码错误";
                model.addAttribute("msg",msg);

                return "admin_login";
            }
        }
    }
    @RequestMapping("/ulogin")
    public String userLogin(){
        return "login";
    }

    @RequestMapping("/vital")
    public  String doorvital(){
        return "door_vital";
    }





    @RequestMapping("/list")
    public String AdminList() {
        return "admin_list";
    }
    @GetMapping("/api/search")
    @ResponseBody
    public SearchAdminResponse searchAdminResponse(@RequestParam("pageNum") Integer pageNum,
                                                  @RequestParam("pageSize") Integer pageSize) {
        return adminService.findAllAdmin(pageNum, pageSize);
    }
    @GetMapping("/api/searchLike")
    @ResponseBody
    public SearchAdminResponse searchAdminResponseLike(@RequestParam("keywords") String keywords,
                                                     @RequestParam("pageNum") Integer pageNum,
                                                     @RequestParam("pageSize") Integer pageSize) {
        return adminService.findAdminLikeName(keywords, pageNum, pageSize);
    }
    @DeleteMapping("/delete")
    @ResponseBody
    public Boolean deleteAdmin(@RequestParam("id") String id) {
        int deleted = adminService.deleteAdmin(id);
        return deleted > 0;
    }

    @RequestMapping("/toStreetMap")
    public String toMap1(){
        return "admin_street_map";
    }

    @RequestMapping("/toDoorMap")
    public String toMap2(){
        return "admin_door_map";
    }

    @RequestMapping("/toDistrictMap")
    public String toMap3(){
        return "admin_district_map";
    }


}
